package com.neu.view.impl;

import com.neu.dao.TradeDao;
import com.neu.dao.impl.TradeDaoImpl;
import com.neu.util.PrintMessage;
import com.neu.util.ScaUtil;
import com.neu.view.UserView;

import java.util.List;
import java.util.Map;

public class TradeView extends UserView {

    private TradeDao tradeDao = new TradeDaoImpl();

    @Override
    public void login(String name, String pwd) {
        return ;
    }

    @Override
    public void register(String name, String sex, String birthday, String pwd, String client_tel) {
        return ;
    }

    /**
     * 菜单
     */
    @Override
    public void menu() {
        while(true){
            System.out.println("1:添加账本记录");
            System.out.println("2:修改账本记录");
            System.out.println("3:查询账本记录");
            System.out.println("4:删除账本记录");
            System.out.println("5:返回上一级");
            System.out.println("输入:");
            String sr = ScaUtil.getScanner().next();
            switch (sr){
                case "1":
                    addTrade();
                    break;
                case "2":
                    updateTrade();
                    break;
                case "3":
                    excuteQueryTrade();
                    break;
                case "4":
                    deleteTrade();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("请合法输入");
                    break;
            }
        }
    }

    /**
     * 删除删除账本记录
     */
    private void deleteTrade() {
        excuteQueryTradeAll();
        System.out.println("确认删除输入:1");
        String choose = ScaUtil.getScanner().next();
        if( !"1".equals(choose) ){
            return;
        }
        System.out.println("输入要删除的账本记录编号");
        String trade_no = ScaUtil.getScanner().next();
        int row = tradeDao.deleteTradeByNo(trade_no);
        PrintMessage.printNum(row);
    }

    /**
     * 查询账本记录
     */
    private void excuteQueryTrade() {
        while(true){
            System.out.println("1:查询所有");
            System.out.println("2:根据交易时间查询账本信息");
            System.out.println("3:返回上一级");
            String choose = ScaUtil.getScanner().next();
            switch (choose){
                case "1":
                    excuteQueryTradeAll();
                    break;
                case "2":
                    excuteQueryTradeByDate();
                    break;
                case "3":
                    return;
                default:
                    System.out.println("请合法输入");
                    break;
            }
        }
    }

    /**
     * 根据交易时间查询账本信息
     */
    private void excuteQueryTradeByDate() {
        System.out.println("输入beginDate");
        String beginDate = ScaUtil.getScanner().next();
        System.out.println("输入endDate");
        String endDate = ScaUtil.getScanner().next();
        List<Map<String,Object>> list = tradeDao.exqueryTradeByDate(this.getMap().get("client_no").toString(),beginDate,endDate);
        PrintMessage.printTradeList(list);
    }

    /**
     * 查询当前用户所有账本信息
     */
    private void excuteQueryTradeAll() {
       List<Map<String, Object>> list = tradeDao.exqueryTrade(this.getMap().get("client_no").toString());
       PrintMessage.printTradeList(list);
    }

    /**
     * 添加账本信息
     */
    private void addTrade() {
        System.out.println("输入交易类型(1存,2取,3代收代扣,4代发工资,5手续费)");
        String trade_code = ScaUtil.getScanner().next();
        System.out.println("输入交易日期");
        String trade_date = ScaUtil.getScanner().next();
        System.out.println("输入交易金额");
        String amount = ScaUtil.getScanner().next();
        String trade_client_no = this.getMap().get("client_no").toString();
        int row = tradeDao.insertTrade(trade_code,trade_date,amount,trade_client_no);
        PrintMessage.printNum(row);
    }

    /**
     * 修改当前用户账本信息
     */
    private void updateTrade() {
         excuteQueryTradeAll();
         System.out.println("确认修改输入:1");
         String sr = ScaUtil.getScanner().next();
         if( !"1".equals(sr) ){
             return;
         }
         System.out.println("输入要修改的账本记录编号");
         String trade_no = ScaUtil.getScanner().next();
         System.out.println("输入交易金额");
         String amount = ScaUtil.getScanner().next();
         System.out.println("输入交易日期");
         String trade_date = ScaUtil.getScanner().next();
         System.out.println("输入交易码(1存 2取 3代收代扣 4代发工资 5手续费)");
         String trade_code = ScaUtil.getScanner().next();
         int row = tradeDao.updateTrade(trade_no,amount,trade_date,trade_code);
         PrintMessage.printNum(row);
    }
}
